const { BrowserWindow, ipcMain } = require('electron');
const path = require('path');

class ProgressDialog {
  constructor() {
    this.window = null;
    this.isVisible = false;
  }

  show(title = '下载中...') {
    if (this.window) {
      this.window.show();
      return;
    }

    this.window = new BrowserWindow({
      width: 400,
      height: 180,
      resizable: false,
      minimizable: false,
      maximizable: false,
      alwaysOnTop: true,
      frame: false, // 去掉窗口框架，让界面更简洁
      title: title,
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false
      }
    });

    // 简洁的HTML进度条界面
    const html = `
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <style>
            body {
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
                margin: 0;
                padding: 20px;
                background: white;
                display: flex;
                flex-direction: column;
                justify-content: center;
                height: 100vh;
                box-sizing: border-box;
            }

            .title {
                font-size: 16px;
                color: #333;
                margin-bottom: 15px;
                text-align: center;
            }
            .progress-container {
                width: 100%;
                height: 8px;
                background: #e0e0e0;
                border-radius: 4px;
                overflow: hidden;
                margin-bottom: 10px;
            }
            .progress-bar {
                height: 100%;
                background: linear-gradient(90deg, #4CAF50, #45a049);
                width: 0%;
                transition: width 0.3s ease;
                border-radius: 4px;
            }
            .progress-text {
                font-size: 12px;
                color: #666;
                text-align: center;
                margin-top: 5px;
            }
            .details {
                font-size: 11px;
                color: #999;
                text-align: center;
                margin-top: 5px;
            }
        </style>
    </head>
    <body>
        <div class="title" id="title">${title}</div>
        <div class="progress-container">
            <div class="progress-bar" id="progressBar"></div>
        </div>
        <div class="progress-text" id="progressText">0%</div>
        <div class="details" id="details">准备下载...</div>
        
        <script>
            const { ipcRenderer } = require('electron');
            
            ipcRenderer.on('progress-update', (event, data) => {
                const { percent, downloaded, total, speed } = data;
                
                const progressBar = document.getElementById('progressBar');
                const progressText = document.getElementById('progressText');
                const details = document.getElementById('details');
                
                // 确保进度百分比在合理范围内
                const safePercent = Math.max(0, Math.min(100, percent || 0));
                progressBar.style.width = safePercent + '%';
                progressText.textContent = Math.round(safePercent) + '%';
                
                if (downloaded && total) {
                    const downloadedMB = (downloaded / 1024 / 1024).toFixed(1);
                    const totalMB = (total / 1024 / 1024).toFixed(1);
                    let detailsText = \`\${downloadedMB} MB / \${totalMB} MB\`;
                    
                    if (speed && speed > 0) {
                        const speedMB = (speed / 1024 / 1024).toFixed(1);
                        detailsText += \` (\${speedMB} MB/s)\`;
                    }
                    
                    details.textContent = detailsText;
                } else if (downloaded) {
                    const downloadedMB = (downloaded / 1024 / 1024).toFixed(1);
                    details.textContent = \`已下载 \${downloadedMB} MB\`;
                } else {
                    details.textContent = '正在连接...';
                }
            });
        </script>
    </body>
    </html>
    `;

    this.window.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(html));
    this.isVisible = true;

    this.window.on('closed', () => {
      this.window = null;
      this.isVisible = false;
    });
  }

  updateProgress(percent, downloaded = 0, total = 0, speed = 0) {
    if (this.window && this.isVisible) {
      this.window.webContents.send('progress-update', {
        percent,
        downloaded,
        total,
        speed
      });
    }
  }

  close() {
    if (this.window) {
      this.window.close();
      this.window = null;
      this.isVisible = false;
    }
  }

  setTitle(title) {
    if (this.window) {
      this.window.setTitle(title);
    }
  }
}

module.exports = ProgressDialog;